<template>
	<view v-show="shareShow" class="animated">
		<view class="share-mak" ></view>
		<view class="sharebox">
			<view class="uni-share-title">分享到</view>
			<scroll-view class="scroll-v"  scroll-x >
				<view class="share-body" v-if="share.providerList.length > 0">
					<block v-for="(value,index) in share.providerList" :key="index">
						<view class="share-item1" hover-class="share-item-hover" @tap="shareTap(value)">
							<view class="icon iconfont"
							 :class="[value.icon,value.zclass]"></view>
							<view>{{value.name}}</view>
						</view>
					</block>
					
					
					
				</view>
				
			</scroll-view>
			<view class="uni-share-bottom" @tap="shareMsk">取消</view>
		</view>
	</view>
</template>

<script>
	export default {
		props:['shareShow','share'],
		data() {
			return {
				
			}
		},
		onLoad(e) {
			console.log(this.share)
		},
		//点击导航按钮
		onNavigationBarButtonTap(e){
			if(e.index ==0){
				this.$emit('shareMsk',this.shareShow)
			}
		},
		
		methods: {
			shareMsk(){
				this.$emit('shareMsk',this.shareShow)
			},
			async shareTap(e) {
				console.log('分享通道:'+ e.id +'； 分享类型:' + this.share.shareType);
				
				if(!this.share.shareText && (this.share.shareType === 1 || this.share.shareType === 0)){
					uni.showModal({
						content:'分享内容不能为空',
						showCancel:false
					})
					return;
				}
				
				if(!this.share.image && (this.share.shareType === 2 || this.share.shareType === 0)){
					uni.showModal({
						content:'分享图片不能为空',
						showCancel:false
					})
					return;
				}
				
				let shareOPtions = {
					provider: e.id,
					scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面，“WXSenceTimeline”分享到朋友圈，“WXSceneFavorite”分享到微信收藏     
					type: this.shareType,
					success: (e) => {
						console.log('success', e);
						uni.showModal({
							content: '已分享',
							showCancel:false
						})
					},
					fail: (e) => {
						console.log('fail', e)
						uni.showModal({
							content: e.errMsg,
							showCancel:false
						})
					},
					complete:function(){
						console.log('分享操作结束!')
					}
				}
				
				switch (this.share.shareType){
					case 0:
						shareOPtions.summary = this.share.shareText;
						shareOPtions.imageUrl = this.share.image;
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.href = 'https://uniapp.dcloud.io';
						break;
					case 1:
						shareOPtions.summary = this.share.shareText;
						break;
					case 2:
						shareOPtions.imageUrl = this.share.image;
						break;
					case 5:
						shareOPtions.imageUrl = this.share.image ? this.share.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.miniProgram = {
							id:'gh_33446d7f7a26',
							path:'/pages/tabBar/component/component',
							webUrl:'https://uniapp.dcloud.io',
							type:0
						};
						break;
					default:
						break;
				}
				
				if(shareOPtions.type === 0 && plus.os.name === 'iOS'){//如果是图文分享，且是ios平台，则压缩图片 
					shareOPtions.imageUrl = await this.compress();
				}
				if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq，则必须加上href和title
					shareOPtions.href = 'https://uniapp.dcloud.io';
					shareOPtions.title = '欢迎体验uniapp';
				}
				uni.share(shareOPtions);
			},
			compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
				console.log('开始压缩');
				let img = this.share.image;
				return new Promise((res) => {
					var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
					console.log('after' + localPath);
					// 压缩size
					plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
						entry.file((file) => {// 可通过entry对象操作图片 
							console.log('getFile:' + JSON.stringify(file));
							if(file.size > 20480) {// 压缩后size 大于20Kb
								plus.zip.compressImage({
									src: img,
									dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
									width: '10%',
									height: '10%',
									quality: 1,
									overwrite: true
								}, (event) => {
									console.log('success zip****' + event.size);
									let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
									res(newImg);
								}, function(error) {
									uni.showModal({
										content:'分享图片太大,需要请重新选择图片!',
										showCancel:false
									})
								});
							}
						});
					}, (e) => {
						console.log('Resolve file URL failed: ' + e.message);
						uni.showModal({
							content:'分享图片太大,需要请重新选择图片!',
							showCancel:false
						})
					});
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
.share-mak{
		z-index: 100;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		top: 0;
		background-color: #000;
		opacity: 0.3;
	}
.sharebox{
	background-color: #fff;
	z-index: 101;
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	.uni-share-title,.uni-share-bottom{
		text-align: center;
		padding: 20upx 0;
	}
	.share-body{
		display: flex;
		// justify-content: space-between;
		align-items: center;
		width: 100%;
		height: 200upx;
		line-height: 200upx;
		white-space: nowrap;
		border-bottom: 1px solid #B2B2B2;
		.share-item1{
			display: inline-flex;
			width:25%;
			flex-direction: column;
			align-items: center;
			&>view:first-child{
				width: 100upx;
				height: 100upx;
				border: 1px solid;
				border-radius: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 50upx;
				color: #FFFFFF;
			}
			&>view:nth-of-type(2){
				color: #666666;
				text-align: center;
			}
			.share-wx{
				background-color: #2AD19B;
			}
			.share-py{
				background-color: #514D4C;
			}
			.share-wb{
				background-color: #EE5E5E;
			}
			.share-qq{
				background-color: #5280CE;
			}
		}
	}
	.share-item-hover{
		background-color: #EEEEEE;
	}
		
}
.scroll-v {
	   height: 100%;
	}
</style>
